<?php
/**
 * @file tryapplyuserinfo.php $
 * @author LiangJian (liangjian@haibao.com) $
 * @date 2015-4-21 下午3:51:52 $
 * @brief 试用中心申请用户业务层 $
 */
namespace haibao\cms\business;

class TryApplyUserInfo extends \haibao\cms\business\Base{
	
	/**
	 * 根据用户id查询用户填写过的用户个人信息
	 * 
	 * @return array
	 */
	public static function getUserById($userId){
		$data = new \haibao\cms\data\TryApplyUserInfo();
		return $data->getUserById($userId);
	}
	
	public static function addUserInfo($model){
		$model->Hope = join(',', explode(',', $model->Hope));
		$model->Sex = (int)$model->Sex;
		$model->LastModifyTime = new \DateTime();
		$data = new \haibao\cms\data\TryApplyUserInfo();
		$data->add($model);
	}
	
	/**
	 * @param \haibao\cms\model\data\TryApplyUserInfo $model
	 */
	public static function saveUserInfo($model){
		$model->Hope = join(',', explode(',', $model->Hope));
		$model->Sex = (int)$model->Sex;
		$model->LastModifyTime = new \DateTime();
		
		$data = new \haibao\cms\data\TryApplyUserInfo();
        
		if($model->Id){
            $data->updateByUserId($model);
		}else{
            $data->addUserInfo($model);
		}
	}
	
	public static function checkForm(){
		if(!$model->UserId){
			throw new BusinessException('用户ID不能为空');
		}
		if(!$model->RealName){
			throw new BusinessException('收货人姓名不能为空');
		}
		if(!$model->Email){
			throw new BusinessException('收货人邮箱不能为空');
		}
		if(!$model->Phone){
			throw new BusinessException('收货人手机号码不能为空');
		}
		if(!$model->Province || !$model->City || !$model->Area || !$model->Address){
			throw new BusinessException('收货人详细地址不完全');
		}
		if(!preg_match('/^[0-9]\d{5}$/', $model->ZipCode)){
			throw new BusinessException('邮政编码格式错误！');
		}
		if(!preg_match('/^1[3|4|5|7|8][0-9]\d{8,8}$/', $model->Phone)){
			throw new BusinessException('手机号码格式错误！');
		}
		if(!filter_var($model->Email, FILTER_VALIDATE_EMAIL)){
			throw new BusinessException('邮箱格式错误！');
		}
	}
}